package oldVersion.storage;

/**
 *@author Adam
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TCPSocketStorage {

	public String my_serverHost;
	public int my_serverPort;
	public Storage parent;

	public TCPSocketStorage(String the_serverHost, int the_serverPort, Storage myParent) {
		my_serverHost = the_serverHost;
		my_serverPort = the_serverPort;
		parent = myParent;
	}

	public void sendMessage(String a_message) {
		try {
			Socket toServer = new Socket(my_serverHost, my_serverPort);
			PrintWriter out = new PrintWriter(toServer.getOutputStream(), true);
			out.println(a_message);
			BufferedReader in = new BufferedReader(new InputStreamReader(toServer.getInputStream()));
			String msg = in.readLine();
			System.out.println("Reply from server: " + msg);
			parse(msg);
			out.close();
			toServer.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (SecurityException se) {
			se.printStackTrace();
		}
	}

	public void parse(String msg){
		System.out.println("rmi server at " + msg);
		parent.setRMIServerIP(msg);
		parent.openRMI();
	}
}

